﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using qtAiGamesLibrary;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using System.Runtime.InteropServices;

namespace Flos
{
    public class TestFlo : MovingEntity 
    {
        SteeringBehaviours _Steering;
        Vector2 _TargetPos;
        List<MovingEntity> _ThreatPos = new List<MovingEntity>();
       
        public int MaxX;
        public int MaxY;
        public int MinX;
        public int MinY;

        public bool Seekit = false;
        public bool Wanderer = false;

        public Microsoft.Xna.Framework.Vector2 TargetPos
	    {
		    get { return _TargetPos; }
		    set { _TargetPos = value; }
	    }

        public TestFlo(Vector2 Location)
        {
            _vPos = Location;
            _Steering = new SteeringBehaviours(this);
        }

        public List<MovingEntity> ThreatPos
        {
            get { return _ThreatPos; }
            set { _ThreatPos = value; }
        }

        // My Texture ;)
        Texture2D myTexture;

        public void Init()
        {
            _MaxSpeed = 100.0f;
            _Mass = 1.0f;
            _vScale = Vector2.One;
            _MaxTurnRate = 2.0f;
        }

        // Functions:
        /// <summary>
        /// Very hardcoded!
        /// </summary>
        public void LoadAssets(ContentManager Content)
        {
            if (Wanderer)
            {
                myTexture = Content.Load<Texture2D>("Killer");
            }
            else
            {
                myTexture = Content.Load<Texture2D>("FloV1_16");
            }
        }


        /// <summary>
        /// Main Logic update function
        /// </summary>
        /// <param name="Time_elapsed"></param>
        public void Update(GameTime Time_elapsed)
        {
            Vector2 SteeringForce;
            
            /* SOME TESTING STUFF
            if (Seekit)
            {
                SteeringForce = _Steering.Seek(TargetPos);
            }
            else
            {
                SteeringForce = _Steering.Flee(TargetPos);
                //SteeringForce = _Steering.Arrive(TargetPos, SteeringBehaviours.Deceleration.Normal);
            }

           

            if (_ThreatPos != null)
            {
                foreach (MovingEntity vThreatPos in _ThreatPos)
                {
                    SteeringForce += _Steering.Evade(vThreatPos);
                }
            }
            */
            if (Wanderer)
            {
                SteeringForce = _Steering.Wander();
            }
            else
            {
                SteeringForce = _Steering.Evade(_ThreatPos[0]);
            }
            
            if (SteeringForce.Length() < 0.00001f)
            {
                _Velocity = _Velocity * (float)(0.99f);
            }

          Vector2 acceleration = SteeringForce / (float)_Mass;

            _Velocity += acceleration * (float)Time_elapsed.ElapsedGameTime.TotalSeconds;

            if (_Velocity.Length() > _MaxSpeed)
            {
                _Velocity.Normalize();
                _Velocity = _Velocity * (float)_MaxSpeed;
            }

            // Update Pos
            _vPos += _Velocity * (float)Time_elapsed.ElapsedGameTime.TotalSeconds;

            

            // Check for bounce.
            if (_vPos.X > MaxX)
            {
                _Velocity.X *= -1;
                _vPos.X = MaxX;
            }

            else if (_vPos.X < MinX)
            {
                _Velocity.X *= -1;
                _vPos.X = MinX;
            }

            if (_vPos.Y > MaxY)
            {
                _Velocity.Y *= -1;
                _vPos.Y = MaxY;
            }

            else if (_vPos.Y < MinY)
            {
                _Velocity.Y *= -1;
                _vPos.Y = MinY;
            }

            if (_Velocity.LengthSquared() > 0.0000001f)
            {
                _Heading = _Velocity;
                _Heading.Normalize();

                _Side = new Vector2(-_Heading.Y, _Heading.X);
            }

        }

        /// <summary>
        /// Draws the flo
        /// </summary>
        public void Draw(GameTime Time_elapsed, SpriteBatch spriteBatch)
        {   
            spriteBatch.Draw(myTexture, _vPos, Color.White);

           
        }

        public void DrawDebug(GraphicsDevice GD, SpriteBatch spriteBatch)
        {
            if (Wanderer)
            {
                // Draw Steering behaviours debug info
                _Steering.DrawDebug(GD, spriteBatch);
            }
        }


    }
}
